package com.dementors.ui.fragment;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.dementors.R;
import com.dementors.base.BaseFragment;
import com.dementors.data.API;
import com.dementors.model.Dress;
import com.dementors.net.http.okhttp.callback.OkHttpCallback;
import com.dementors.net.http.okhttp.request.OkHttpRequest;
import com.dementors.ui.activity.ItemActivity;
import com.dementors.ui.adapter.DressAdapter;
import com.dementors.view.PullToRefreshRecyclerView;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.ArrayList;


/**
 * ***********************
 * author:raochen
 * time:2015/12/1.
 * name:特卖童装的Fragment
 * step:
 */
public class DressFragment extends BaseFragment {
    //返回顶部的imageview的容器控件
    private RelativeLayout rl_totop;
    //自定义的RecyclerView
    private com.dementors.view.PullToRefreshRecyclerView prrv;
    //url集合
    ArrayList<String> urls1;
    ArrayList<String> urls2;
    //type:fragment类型
    int type;
    //fragment当前页
    int page = 1;
    //TOTOP的imageview
    private ImageView iv_totop;
    //列表数据的集合
    private ArrayList<Dress.DressShow> dressShows;
    //适配器
    private DressAdapter mAdapter;


    //handler耗时操作执行
    private Handler handler = new Handler();

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_dress;
    }

    @Override
    protected void initViews() {
        prrv = (PullToRefreshRecyclerView) view.findViewById(R.id.prrv);
        iv_totop= (ImageView) view.findViewById(R.id.iv_totop);
        rl_totop= (RelativeLayout) view.findViewById(R.id.rl_totop);

    }

    @Override
    protected void initData() {

        //url初始化
        initUrl();
        //type值
        getType();
        dressShows = new ArrayList<>();
        mAdapter = new DressAdapter(getActivity());
        mAdapter.setList(dressShows);


        prrv.setAdapter(mAdapter);
        //注册监听器
        registerListener();

        //网络访问下载数据
        HttpGet();


    }

    /**
     * 获取fragment类型
     */
    private void getType() {
        type = (int) getArguments().get("type");
    }

    /**
     * 初始化url集合
     */
    private void initUrl() {
        urls1 = new ArrayList<>();
        urls2 = new ArrayList<>();
        urls1.add(API.DRESS_LIST_DATA_1);
        urls1.add(API.SHOE_LIST_DATA_1);
        urls1.add(API.BABYTHINGS_LIST_DATA_1);
        urls1.add(API.TOY_LIST_DATA_1);
        urls1.add(API.WOMAN_DRESS_LIST_DATA_1);
        urls1.add(API.HOUSE_LIST_DATA_1);
        urls1.add(API.FOOD_LIST_DATA_1);
        urls1.add(API.BEAUTY_LIST_DATA_1);
        urls2.add(API.DRESS_LIST_DATA_2);
        urls2.add(API.SHOE_LIST_DATA_2);
        urls2.add(API.BABYTHINGS_LIST_DATA_2);
        urls2.add(API.TOY_LIST_DATA_2);
        urls2.add(API.WOMAN_DRESS_LIST_DATA_2);
        urls2.add(API.HOUSE_LIST_DATA_2);
        urls2.add(API.FOOD_LIST_DATA_2);
        urls2.add(API.BEAUTY_LIST_DATA_2);

    }


    /**
     * 设置监听器
     */
    private void registerListener() {
        prrv.setRefreshLoadMoreListener(new PullToRefreshRecyclerView.RefreshLoadMoreListener() {
            @Override
            public void onRefresh() {
                Log.i("oye", "到顶部");
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        prrv.stopRefresh();
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore() {
                HttpGet2();
                rl_totop.setVisibility(View.VISIBLE);
                iv_totop.setClickable(true);
            }

            @Override
            public void atTop() {
                rl_totop.setVisibility(View.INVISIBLE);
                iv_totop.setClickable(false);
            }

            @Override
            public void showButton() {
                rl_totop.setVisibility(View.VISIBLE);
                iv_totop.setClickable(true);
            }
        });
        iv_totop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prrv.scrollToTop();
                rl_totop.setVisibility(View.INVISIBLE);
                iv_totop.setClickable(false);
            }
        });

        //点击事件监听
        mAdapter.setOnItemClickListener(new DressAdapter.onItemClickListener() {
            @Override
            public void itemClick(View view, int position) {
                Log.i("oye", "点了没" + dressShows.get(position).event_id);
                Intent intent = new Intent(getActivity(), ItemActivity.class);
                intent.putExtra("id", dressShows.get(position).event_id);
                startActivity(intent);
            }
        });

    }

    private void HttpGet2() {

        page++;
        new OkHttpRequest.Builder().url(urls1.get(type) + page + urls2.get(type)).get(new OkHttpCallback<Dress>() {

            @Override
            public void onError(Request request, Exception e) {
                Log.i("oye", "访问失败");
            }

            @Override
            public void onResponse(Response response, Dress data) {
                Log.i("oye", "加载更多");
                dressShows.addAll(data.martshows);
                mAdapter.setList(dressShows);
                mAdapter.notifyDataSetChanged();
                prrv.setLoadMoreCompleted();
            }
        });

    }

    /**
     * 网络访问
     */
    private void HttpGet() {
        new OkHttpRequest.Builder().url(urls1.get(type) + page + urls2.get(type)).get(new OkHttpCallback<Dress>() {

            @Override
            public void onError(Request request, Exception e) {
                Log.i("oye", "访问失败");
            }

            @Override
            public void onResponse(Response response, Dress data) {

                dressShows.addAll(data.martshows);
                mAdapter.setList(dressShows);
                mAdapter.notifyDataSetChanged();
            }
        });

    }

}

